package com.hmdp.controller;


import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.service.IFollowService;
import com.hmdp.utils.UserHolder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Slf4j
@RestController
@RequestMapping("/follow")
@RequiredArgsConstructor
public class FollowController {

    private final IFollowService followService;

    /**
     * 关注或取关用户
     * @param followUserId
     * @param isFollow
     * @return
     */
    @PutMapping("/{id}/{isFollow}")
    public Result follow(@PathVariable("id") Long followUserId,@PathVariable("isFollow") Boolean isFollow) {
        log.info(isFollow ? "关注用户" : "取关用户" + "userId：{}",followUserId);
        followService.follow(followUserId,isFollow);
        return Result.ok();
    }

    /**
     * 查看是否关注用户
     * @param followUserId
     * @return
     */
    @GetMapping("/or/not/{id}")
    public Result follow(@PathVariable("id") Long followUserId) {
        log.info("查看是否关注用户：{}",followUserId);
        Boolean isFollow = followService.isFollow(followUserId);
        return Result.ok(isFollow);
    }

    /**
     * 查看共同关注用户
     * @param id
     * @return
     */
    @GetMapping("/common/{id}")
    public Result followCommons(@PathVariable("id") Long id) {
        log.info("查看共同关注用户：{},{}", UserHolder.getUser().getId() ,id);
        List<UserDTO> userDTOList = followService.followCommons(id);
        return Result.ok(userDTOList);
    }

}
